package jp.sourceforge.akjrcp.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

 public  final class FFMpegUtils {

		public static boolean isFFMpegAvailable(String path){
			if(path!=null && !path.equals("")){
				if(path.endsWith("ffmpeg.exe") && new File(path).exists()){
				return true;
				}
			}
			return false;
		}
		
	public static String toAVSScript(String path){
		return "DirectShowSource(\""+path+"\")";
	}
	public static boolean shouldUseAvisynth(File file){
		String notsupport[]={"flv"};
		for (int i = 0; i < notsupport.length; i++) {
			if(file.getName().endsWith("."+notsupport[i])){
				return false;
			}
		}
		return true;
	}
	public static boolean isAvisynthAvailable(File ffmpeg){
		File tmp=null;
		try {
			tmp = File.createTempFile("avisynth",".avs");
			FileWriter writer=new FileWriter(tmp);
			writer.write("BlankClip()");
			writer.close();
			Runtime runtime=Runtime.getRuntime();
			Process p=runtime.exec(new String[]{ffmpeg.getAbsolutePath(),"-i",tmp.getAbsolutePath()});
			SimpleProcessPrinter ffmpegMonitor=new SimpleProcessPrinter();
			ProcessMonitor monitor=new ProcessMonitor(p,ffmpegMonitor);
			monitor.startMonitor();
			try {
				p.waitFor();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tmp.delete();
			String log=ffmpegMonitor.getLog();
			if(log.indexOf("Video: rawvideo")!=-1){
				return true;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			tmp.delete();
			e.printStackTrace();
		}
		return false;
	}
	


}
